1 module hip.event.handlers.input_listener;
2 public import hip.api.input.button;
3 public import hip.api.input.keyboard;
4 public import hip.api.input.mouse;
5 import hip.event.dispatcher;
6 import hip.event.api;
7 import hip.event.handlers.keyboard;
8 import hip.event.handlers.mouse;
9 
10 
11 
12 class HipInputListener
13 {
14     protected HipButton[] touchListeners;
15     protected HipButton[] keyboardListeners;
16 
17     protected ScrollListener[] scrollListeners;
18     protected TouchMoveListener[] moveListeners;
19 
20     protected KeyboardHandler keyboard;
21     protected HipMouse mouse;
22 
23     this(EventDispatcher dispatcher)
24     {
25         this.keyboard = dispatcher.keyboard;
26         this.mouse = dispatcher.mouse;
27     }
28 
29     const(HipButton)* addKeyboardListener(HipKey key, 
30         HipInputAction action,
31         HipButtonType type = HipButtonType.down,
32         AutoRemove remove = AutoRemove.no
33         )
34     {
35         keyboardListeners~= HipButton(cast(ushort)key, type, action, remove);
36         
37         return &keyboardListeners[$-1];
38     }
39 
40     const(HipButton)* addTouchListener(HipMouseButton btn, 
41         HipInputAction action,
42         HipButtonType type = HipButtonType.down,
43         AutoRemove remove = AutoRemove.no
44         )
45     {
46         touchListeners~= HipButton(cast(ushort)btn, type, action, remove);
47         return &touchListeners[$-1];
48     }
49 
50     const(ScrollListener)* addScrollListener(HipScrollAction onScroll, AutoRemove isAutoRemove = AutoRemove.no)
51     {
52         scrollListeners~= ScrollListener(onScroll, isAutoRemove);
53         return &scrollListeners[$-1];
54     }
55     const(TouchMoveListener)* addTouchMoveListener(HipTouchMoveAction onTouchMove, AutoRemove isAutoRemove = AutoRemove.no)
56     {
57         moveListeners~= TouchMoveListener(onTouchMove, isAutoRemove);
58         return &moveListeners[$-1];
59     }
60 
61     /**
62     *   Mainly used for the scriptInputListener
63     */
64     void clearAll()
65     {
66         touchListeners.length = 0;
67         scrollListeners.length = 0;
68         moveListeners.length = 0;
69         keyboardListeners.length = 0;
70     }
71 
72     import hip.util.array:remove;
73     bool removeKeyboardListener(const(HipButton)* button)
74     {
75         return remove(keyboardListeners, button);
76     }
77     bool removeTouchListener(const(HipButton)* button)
78     {
79         return remove(touchListeners, button);
80     }
81     bool removeScrollListener(const(ScrollListener)* onScroll)
82     {
83         return remove(scrollListeners, onScroll);
84     }
85     bool removeTouchMoveListener(const(TouchMoveListener)* onTouchMove)
86     {
87         return remove(moveListeners, onTouchMove);
88     }
89 
90     void update()
91     {
92         foreach(ref key; keyboardListeners)
93         {
94             bool shouldExecute = false;
95             final switch(key.type) with(HipButtonType)
96             {
97                 case down:
98                     shouldExecute = keyboard.isKeyJustPressed(cast(char)key.id);
99                     break;
100                 case up:
101                     shouldExecute = keyboard.isKeyJustReleased(cast(char)key.id);
102                     break;
103             }
104             if(shouldExecute)
105             {
106                 key.action(keyboard.getMetadata(cast(char)key.id));
107                 if(key.isAutoRemove)
108                     removeKeyboardListener(&key);
109             }
110         }
111 
112         if(mouse.getDeltaPosition().magSquare != 0)
113         {
114             import hip.api.input;
115             import hip.console.log;
116             float[2] pos = getWorldTouchPosition();
117             int x = cast(int)pos[0], y = cast(int)pos[1];
118             foreach(ref move; moveListeners)
119             {
120                 move.action(x, y);
121                 if(move.isAutoRemove)
122                     removeTouchMoveListener(&move);
123             }
124         }
125         auto scroll = mouse.getScroll();
126         if(scroll.magSquare != 0)
127         {
128             foreach(ref listener; scrollListeners)
129             {
130                 listener.action(cast(float[3])scroll);
131                 if(listener.isAutoRemove)
132                     removeScrollListener(&listener);
133             }
134         }
135 
136         foreach(ref touch; touchListeners)
137         {
138             bool shouldExecute = false;
139             final switch(touch.type) with(HipButtonType)
140             {
141                 case down:
142                     shouldExecute = mouse.isJustPressed(cast(HipMouseButton)touch.id);
143                     break;
144                 case up:
145                     shouldExecute = mouse.isJustReleased(cast(HipMouseButton)touch.id);
146                     break;
147             }
148             if(shouldExecute)
149             {
150                 touch.action(mouse.getMetadata(cast(HipMouseButton)touch.id));
151                 if(touch.isAutoRemove)
152                     removeTouchListener(&touch);
153             }
154         }
155     }
156 }